package tt.dz.appserver.mvc.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.service.business.ChargerService;
import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.enums.Response;


/**
 * 电桩控制类
 * @author xielf
 *
 */
@RestController
@RequestMapping("/charger")
public class ChargerController {

	/**电桩基本业务接口*/
	@Resource
	private ChargerService chargerService;
	
	/**
	 * 获取站点下空闲桩的数量
	 * @param stationId
	 * @return
	 */
	@RequestMapping(value="/getFreeChargerByStationId",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String getFreeChargerByStationId(
			@RequestParam(value="stationId", required=true) String stationId){
		
		ResultMappingEntity resultMappingEntity= null;
		
		try {
			
			//获取站点下空闲桩的数量
			JSONObject resultJsonObject = chargerService.getFreeChargerNum(stationId);
			
			String code = resultJsonObject.getString("code");
			String msg = resultJsonObject.getString("msg");

			if(Response.SUCCESS.getCode().equals(code)){
				resultJsonObject.remove("code");
				resultJsonObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultJsonObject);
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	
	/**
	 * 获取站点下电桩列表
	 * @param stationId
	 * @return
	 */
	@RequestMapping(value="/getChargerListByStationId",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String getChargerListByStationId(
			@RequestParam(value="stationId", required=true) String stationId){
		
		ResultMappingEntity resultMappingEntity= null;
		
		try {
			
			//获取站点下电桩列表
			JSONObject resultJsonObject = chargerService.getChargerListByStationId(stationId);
			String code = resultJsonObject.getString("code");
			String msg = resultJsonObject.getString("msg");

			if(Response.SUCCESS.getCode().equals(code)){
				resultJsonObject.remove("code");
				resultJsonObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultJsonObject.get("chargerList"));
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	
	/**
	 * 获取电桩详细
	 * @param chargerId
	 * @return
	 */
	@RequestMapping(value="/getChargerDetailByChargerId",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String chargerDetail(
			@RequestParam(value="chargerId", required=true) String chargerId){
		
		ResultMappingEntity resultMappingEntity= null;

		try {
			
			//获取电桩详细
			JSONObject resultJsonObject = chargerService.getChargerDetailByChargerId(chargerId);
			
			String code = resultJsonObject.getString("code");
			String msg = resultJsonObject.getString("msg");

			if(Response.SUCCESS.getCode().equals(code)){
				resultJsonObject.remove("code");
				resultJsonObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultJsonObject.get("chargerDetailVo"));
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 通过二维码号获取电桩详细
	 * @param chargerId
	 * @return
	 */
	@RequestMapping(value="/getChargerDetailByQrCode",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String getChargerDetailByQrCode(
			@RequestParam(value="qrCode", required=true) String qrCode){
		
		ResultMappingEntity resultMappingEntity= null;

		try {
			
			//获取电桩详细
			JSONObject resultJsonObject = chargerService.getChargerDetailByQrCode(qrCode);
			
			String code = resultJsonObject.getString("code");
			String msg = resultJsonObject.getString("msg");

			if(Response.SUCCESS.getCode().equals(code)){
				resultJsonObject.remove("code");
				resultJsonObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultJsonObject.get("chargerDetailVo"));
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 获取枪头状态
	 * @param qrCode
	 * @param plugId
	 * @return
	 */
	@RequestMapping(value="/plugStatus",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String plugStatus(
			@RequestParam(value="chargerId", required=true) String chargerId,
			@RequestParam(value="plugNo", required=true) String plugNo){
		
		ResultMappingEntity resultMappingEntity= null;
		
		try {
			
			//获取枪头状态
			JSONObject resultJsonObject = chargerService.getChargerPlugStatus(chargerId, plugNo);
			
			String code = resultJsonObject.getString("code");
			String msg = resultJsonObject.getString("msg");

			if(Response.SUCCESS.getCode().equals(code)){
				resultJsonObject.remove("code");
				resultJsonObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultJsonObject.get("plugNoDetailVo"));
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 获取当前用户正在使用的设备
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/getUsingCharger",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String getUsingCharger(
			@RequestParam(value="userCard", required=true) String userCard){
		
		ResultMappingEntity resultMappingEntity= null;
		
		try {
			
			//获取当前用户正在使用的设备
			JSONObject resultJsonObject = chargerService.getUsingChargerByUserId(userCard);
			
			String code = resultJsonObject.getString("code");
			String msg = resultJsonObject.getString("msg");

			if(Response.SUCCESS.getCode().equals(code)){
				resultJsonObject.remove("code");
				resultJsonObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultJsonObject.get("usingNum"));
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 获取插头列表
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/getPlugsListByChargerId",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String getPlugsListByChargerId(
			@RequestParam(value="chargerId", required=true) String chargerId){
		
		ResultMappingEntity resultMappingEntity= null;
		
		try {
			
			//获取当前用户正在使用的设备
			JSONObject resultJsonObject = chargerService.getPlugsListByChargerId(chargerId);
			
			String code = resultJsonObject.getString("code");
			String msg = resultJsonObject.getString("msg");

			if(Response.SUCCESS.getCode().equals(code)){
				resultJsonObject.remove("code");
				resultJsonObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultJsonObject.get("plugNoDetailVo"));
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 获取费率
	 * @param rateType
	 * @param chargerId
	 * @return
	 */
	@RequestMapping(value="/getRate",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String getRate(String rateType, String chargerId){
		
		ResultMappingEntity resultMappingEntity = null;
		try {
			resultMappingEntity = chargerService.getRate(chargerId);
		} catch (Exception e) {
			resultMappingEntity =  Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(),e.getMessage());
		}
		
		return resultMappingEntity.toString();
	}
	
}
